﻿using Prism.Navigation.Model;
using Prism.Navigation.Msg;
using Prism.Navigation.Serves;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prism.Navigation.ViewModels
{
    public class LoginViewModel:BindableBase
    {
        private readonly DbContext _dbContext;

        private readonly IEventAggregator _eventAggregator;

        public LoginViewModel(UserSession userSession, DbContext dbContext,IEventAggregator eventAggregator)
        {
            LoginCommand = new DelegateCommand(login);
            CurrentSession = userSession;
            _dbContext = dbContext;
            _eventAggregator = eventAggregator;
        }

        #region 登录命令

        private string account;

        public string Account
        {
            get { return account; }
            set { account = value;RaisePropertyChanged(); }
        }

        private string password;

        public string  Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }


        private UserSession currentSession;

        public UserSession CurrentSession
        {
            get { return currentSession; }
            set { currentSession = value;  RaisePropertyChanged(); }
        }


        public DelegateCommand LoginCommand { get; set; }

        private void login()
        {
            User user=new User() { Account=Account,Password=Password};
            if (_dbContext.FindUser(Account, Password))
            {
                CurrentSession.CurrentUser = user;
                _eventAggregator.GetEvent<LoginMessage>().Publish(user);
            }
            
        }

        #endregion
    }
}
